<?php
namespace zake\app\message;
use zake\app\user\User;


/**
 * Class MessageDto
 * @package zake\app\message
 *
 *
 */
class MessageDto{


    /**
     *
     * @param $message
     *
     * @return self
     * @author liu.bin 2020-09-08 15:52
     */
    public static function parse($message){

        $messArr = json_decode($message,true);
        $messageDto = new self();
        $messageDto->setBody($messArr['body']);
        $messageDto->setType($messArr['type']);
        $messageDto->setFrom($messArr['from']);
        $messageDto->setTo($messArr['to']);
        return $messageDto;
    }


    private $type;


    private $from;


    private $to;


    private $body;

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @param mixed $type
     */
    public function setType($type): void
    {
        $this->type = $type;
    }

    /**
     * @return array
     */
    public function getFrom()
    {
        return $this->from;
    }

    /**
     * @param mixed $from
     */
    public function setFrom($from): void
    {
        $this->from = $from;
    }


    /**
     * 获取 from用户
     *
     * @author liu.bin 2020-09-15 18:16
     */
    public function getFromUser(){

        if(empty($this->from)){
            return null;
        }

        $user = new User();
        $user = $user->userAttr($user,$this->from);
        $user->setSessionId($this->from['sessionId']);
        return $user;
    }

    /**
     * @return User
     */
    public function getTo()
    {
        return $this->to;
    }


    /**
     * @param mixed $to
     */
    public function setTo($to): void
    {
        $this->to = $to;
    }


    /**
     * 获取 to用户
     * @return null|User
     *
     * @author liu.bin 2020-09-15 18:15
     */
    public function getToUser()
    {

        if(empty($this->to)){
            return null;
        }
        $user = new User();
        $user = $user->userAttr($user,$this->to);
        $user->setSessionId($this->to['sessionId']);
        var_dump($this->to);
        return $user;
    }



    /**
     * @return mixed
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * @param mixed $body
     */
    public function setBody($body): void
    {
        $this->body = $body;
    }


}